<template>
  <el-dialog title="编辑标记" :visible.sync="dialogVisible" @close='dialogFormVisible' :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form size="small" :model="form" :rules="formRules" ref="form">
      <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="描述" :label-width="formLabelWidth">
        <el-input v-model="form.describe" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-row>
          <el-button type="primary" @click="openUpload">选择图片</el-button>
          <div v-if="form.icon!=''" class="img">
            <img :src="form.icon" width="100" height="100"/>
          </div>
        </el-row>
      </el-form-item>
      <el-form-item label="悬浮方向" :label-width="formLabelWidth">
        <el-radio-group v-model="form.position">
          <el-radio :label="0">左悬浮</el-radio>
          <el-radio :label="1">右悬浮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="上边距" :label-width="formLabelWidth">
        <el-input v-model="form.t_margin" autocomplete="off"></el-input>
        <el-label :style="{color:'#c5c5c5'}">单位：px</el-label>
      </el-form-item>
      <el-form-item label="左/右边距" :label-width="formLabelWidth">
        <el-input v-model="form.l_margin" autocomplete="off"></el-input>
        <el-label :style="{color:'#c5c5c5'}">单位：px</el-label>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">显示</el-radio>
          <el-radio :label="0">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序号" :label-width="formLabelWidth">
        <el-input v-model="form.sort" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" @click="editCategory" :loading="loading">确 定</el-button>
    </div>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">上传图片</Upload>
  </el-dialog>
</template>

<script>
import MarkApi from '@/api/productMark.js';
import Upload from '@/components/file/Upload';

export default {
  components: {
    Upload
  },
  data() {
    return {
      form: {
        id: 0,
        name: '',
        icon: '',
        describe: '',
        status: 1,
        position: 0,
        t_margin: 0,
        l_margin: 0,
        sort: 0,
      },
      formRules: {
        name: [{required: true, message: '请输入名称', trigger: 'blur'}],
      },
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      /*是否上传图片*/
      isupload: false,
    };
  },
  props: ['open_edit', 'editform'],
  created() {
    this.dialogVisible = this.open_edit;
    this.form.id = this.editform.id;
    this.form.name = this.editform.name;
    this.form.describe = this.editform.describe;
    this.form.status = this.editform.status;
    this.form.icon = this.editform.icon;
    this.form.sort = this.editform.sort;
    this.form.position = this.editform.position;
    this.form.t_margin = this.editform.t_margin;
    this.form.l_margin = this.editform.l_margin;
  },
  methods: {
    /*修改*/
    editCategory() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          MarkApi.editMark(params, true)
            .then(data => {
              self.loading = false;
              self.$message({
                message: '恭喜你，修改成功',
                type: 'success'
              });
              self.dialogFormVisible(true);
            })
            .catch(error => {
              self.loading = false;
            });
        }
      });
    },

    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        })
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        })
      }
    },
    /*上传*/
    openUpload(e) {
      this.type = e;
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      if (e != null && e.length > 0) {
        this.form.icon = e[0].file_path;
      }
      this.isupload = false;
    },

  }
};
</script>

<style></style>
